package com.myapp.ruyixing.ui;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

public class OrderMenuFragment extends ListFragment implements
		OnItemClickListener {

	private String[] items = new String[] { "默认", "距离最近", "费用从低到高", "费用从高到低", "按总体评价排序", "按环境排序", "按服务排序" };
	private String[] items2 = new String[] { "默认", "距离最近" };

	public static OrderMenuFragment newOrderMenuFragment(boolean showPrice) {
		OrderMenuFragment fragment = new OrderMenuFragment();
		Bundle bundle = new Bundle();
		bundle.putBoolean("showPrice", showPrice);
		fragment.setArguments(bundle);

		return fragment;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		boolean showOrder = true;
		if (getArguments() != null) {
			showOrder = getArguments().getBoolean("showPrice", true);
		}
		setListAdapter(new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, showOrder ? items : items2));
		getListView().setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (getActivity() instanceof NearbyStoreActivity) {
			((NearbyStoreActivity) getActivity()).changeOrder(
					getVaule(position), items[position]);
		} else if (getActivity() instanceof FoodListActivity) {
			((FoodListActivity) getActivity()).changeOrder(getVaule(position),
					items[position]);
		}
	}

	private String getVaule(int position) {
		switch (position) {
		case 0:
			return "default";

		case 1:
			return "distance";

		case 2:
			return "priceasc";

		case 3:
			return "pricedesc";

		case 4:
			return "pinjia";

		case 5:
			return "weisheng";

		case 6:
			return "fuwu";

		default:
			return "default";
		}
	}
}
